<template>
  <div>
    <el-card class="roles-container">
      <bread :params1="'权限管理'" :params2="'角色列表'"></bread>
      <div>
        <el-button type="primary" style="    margin-top: 10px;"
          >添加角色</el-button
        >
      </div>

      <el-table border :data="roleslist" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- <pre>{{ scope.row.children[0].authName }}</pre> -->
            <!-- <pre>{{ scope.row.id }}</pre> -->
            <el-row v-for="(item, index) in scope.row.children" :key="index">
              <el-col :span="8">
                <el-tag
                  closable
                  @close="rolesDel(scope.row.id, item.id, scope.row)"
                >
                  {{ item.authName }}</el-tag
                >
              </el-col>
              <el-col :span="16">
                <!-- 二级 -->
                <el-row v-for="(item2, index2) in item.children" :key="index2">
                  <el-col :span="8">
                    <el-tag
                      type="success"
                      closable
                      @close="rolesDel(scope.row.id, item2.id, scope.row)"
                    >
                      {{ item2.authName }}</el-tag
                    >
                  </el-col>
                  <el-col :span="16">
                    <!-- 三级 -->
                    <el-tag
                      v-for="(item3, index3) in item2.children"
                      :key="index3"
                      type="warning"
                      closable
                      @close="rolesDel(scope.row.id, item3.id, scope.row)"
                    >
                      {{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
                <!-- 二级 -->
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="180"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" prop="desc">
          <template slot-scope="scope">
            <!-- <pre>{{ scope.row }}</pre> -->
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >

            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >

            <el-tooltip content="分配角色" placement="top">
              <el-button
                @click="btnRights(scope.row)"
                type="warning"
                icon="el-icon-setting"
                size="mini"
                >分配权限</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 树形组件 -->
    <el-dialog title="提示" :visible.sync="dialogVisibleRights" width="30%">
      <!-- 显示树形组件 -->
      <el-tree
        :data="treeList"
        ref="tree"
        show-checkbox
        node-key="id"
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="defaultArr"
        :props="defaultProps"
        :default-expand-all="true"
      >
      </el-tree>
      <!-- 显示树形组件 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRolesRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { roles_api, rightsDel_api, rights_api, addRoleRights_api } from "@/api";

import { returnMessage } from "@/common/$message.js";
export default {
  data() {
    return {
      roleslist: [],
      dialogVisibleRights: false,
      // 树形组件
      treeList: [],
      //勾选的数据 //  定义选中的id数组
      defaultArr: [],
      defaultProps: {
        children: "children",
        label: "authName"
      },
      //角色id
      roleId: ""
    };
  },
  methods: {
    async rolesapi() {
      const { data: res } = await roles_api();
      // console.log(res.data, 7777777);
      this.roleslist = res.data;
    },
    //9-删除用户角色的权限
    rolesDel(roleId, rightId, rows) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          //点击确认发送删除的请求  携带的参数是一个对象
          const { data: res } = await rightsDel_api({ roleId, rightId });
          const data = returnMessage(res, this.$message);
          // console.log(data, 22222);
          //重新渲染页面  改变传进来的每一行的数据
          rows.children = data;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //分配权限对话框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },

    //点击分配权限 弹出对话框
    async btnRights(rows) {
      //获取当前行的角色id
      this.roleId = rows.id;
      //每点击的时候=都清空一下树形组件框
      this.defaultArr = [];
      this.dialogVisibleRights = true;
      //发送请求
      const { data: res } = await rights_api("tree");
      // console.log(res.data, 8888888888);
      //获取数据
      this.treeList = res.data;
      //调用递归函数
      this.argumTree(rows, this.defaultArr);
    },
    //递归函数
    argumTree(rows, arr) {
      // 没有 children
      if (!rows.children) {
        return arr.push(rows.id);
      }
      //有 children
      rows.children.forEach(item => {
        //继续往下找
        this.argumTree(item, arr);
      });
    },
    //分配权限对话框里面的确认按钮
    async addRolesRights() {
      // 获取权限id--获取所有被选中的权限id：
      // 通过this.$refs.tree.getCheckedKeys()---返回的是目前被选中的叶子节点的key组成的数组
      // 通过this.$refs.tree.getHalfCheckedKeys()---返回的是目前半选中的节点的key组成的数组
      // 定义一个空数组  存储 权限id ----展开运算符----join以,逗号 拼接成字符串
      let rids = [];
      // console.log(this.$refs.tree.getCheckedKeys(), 33333333);
      rids = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()
      ].join();
      //发送请求
      const { data: res } = await addRoleRights_api({
        roleId: this.roleId,
        rids
      });
      //调用 判断发送请求后的函数
      const data = returnMessage(res, this.$message);
      console.log(data, 22222);
      this.dialogVisibleRights = false;
      this.rolesapi();
    }
  },
  created() {
    this.rolesapi();
  }
};
</script>

<style scoped lang="less">
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.roles-container {
  .el-table {
    margin: 15px 0;
  }
}
</style>
